import { useMysql } from "../mysql/index.js";
import { MysqlQueryBuilder } from "../mysql/mysql-query-builder.js";
// 查询所有设备数据
export const getEquipmentAll = async (req, res) => {
  const sql = MysqlQueryBuilder.select("equipments");
  try {
    const result = await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "查询成功",
      data: result,
    });
  } catch (err) {
    console.error("查询失败:", err);
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 分页查询设备
export const getEquipmentArr = async (req, res) => {
  const { page, pageSize, orderBy } = req.body;
  const { rowSql, countSql } = await MysqlQueryBuilder.paginate(
    "equipments",
    ["*"],
    {},
    page,
    pageSize,
    orderBy
  );
  try {
    // 执行查询
    const [rowResult, countResult] = await Promise.all([
      useMysql(rowSql),
      useMysql(countSql),
    ]);
    const total = countResult[0].total;
    const totalPages = Math.ceil(total / pageSize);
    return res.json({
      code: 200,
      row: rowResult,
      total: total,
      message: "Success",
    });
  } catch (error) {
    return res.status(404).json({
      code: 404,
      message: "File not found",
    });
  }
};
// 更新设备
export const updataEquipmentById = async (req, res) => {
  const {
    EquipmentID,
    EquipmentName,
    EquipmentType,
    LastInspectionDate,
    Status,
    ImagePath,
  } = req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.update(
    "equipments",
    {
      EquipmentID: EquipmentID,
      EquipmentName: EquipmentName,
      EquipmentType: EquipmentType,
      LastInspectionDate: LastInspectionDate || null,
      Status: Status,
      ImagePath: ImagePath,
    },
    {
      EquipmentID: EquipmentID,
    }
  );
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "修改成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 删除设备
export const deleteEquipmentById = async (req, res) => {
  const id = req.params.id;
  const sql = MysqlQueryBuilder.delete("equipments", {
    EquipmentID: id,
  });
  try {
    const result = await useMysql(sql); // 执行SQL
    res.status(200).json({
      code: 200,
      message: "删除成功",
    });
  } catch (err) {
    res.status(500).json({
      code: 500,
      message: "服务器内部错误",
      error: err.message,
    });
  }
};
// 删除设备(多id批量)
export const deleteEquipmentMoreById = async (req, res) => {
  const ids = req.body;
  // 构造 SQL 查询语句
  const sql = MysqlQueryBuilder.delete("equipments", {
    EquipmentID: ids,
  });
  try {
    console.log(ids);
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "删除成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
// 查找设备(单条件)
export const SearchEquipmentKey = async (req, res) => {
  const { SearchKey, SearchValue, page, pageSize, orderBy } = req.body;
  // 中间转化
  const TemObject = {};
  TemObject[SearchKey] = SearchValue;
  const { rowSql, countSql } = await MysqlQueryBuilder.paginate(
    "equipments",
    ["*"],
    TemObject,
    page,
    pageSize,
    orderBy
  );
  try {
    // 执行查询
    const [rowResult, countResult] = await Promise.all([
      useMysql(rowSql),
      useMysql(countSql),
    ]);
    const total = countResult[0].total;
    const totalPages = Math.ceil(total / pageSize);
    return res.json({
      code: 200,
      row: rowResult,
      total: total,
      message: "Success",
    });
  } catch (error) {
    return res.status(404).json({
      code: 404,
      message: "File not found",
    });
  }
};
// 添加设备
export const addEquipment = async (req, res) => {
    const {
        EquipmentID,
        EquipmentName,
        EquipmentType,
        LastInspectionDate,
        Status,
        ImagePath,
      } = req.body;
      // 构造 SQL 查询语句
      const sql = MysqlQueryBuilder.insert(
        "equipments",
        {
          EquipmentID: EquipmentID,
          EquipmentName: EquipmentName,
          EquipmentType: EquipmentType,
          LastInspectionDate: LastInspectionDate || null,
          Status: Status,
          ImagePath: ImagePath,
        },
      );
  try {
    await useMysql(sql);
    res.status(200).send({
      code: 200,
      message: "添加成功",
    });
  } catch (err) {
    res.status(500).send({
      code: 500,
      message: "服务器内部错误",
    });
  }
};
